package com.droid.lib.controls.widget.spinner;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.PopupWindow;

/**
 * Author: cuixiaohua
 * Created on: 2017-11-30 15:30
 */
public class MyPopupWindow extends PopupWindow {
    private int statusBarHeight;

    public MyPopupWindow(Context context) {
        super(context, null);
    }

    /**
     * <p>Create a new empty, non focusable popup window of dimension (0,0).</p>
     * <p>
     * <p>The popup does provide a background.</p>
     *
     * @param context
     * @param attrs
     */
    public MyPopupWindow(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * <p>Create a new empty, non focusable popup window of dimension (0,0).</p>
     * <p>
     * <p>The popup does provide a background.</p>
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public MyPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * <p>Create a new, empty, non focusable popup window of dimension (0,0).</p>
     * <p>
     * <p>The popup does not provide a background.</p>
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     * @param defStyleRes
     */
    public MyPopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * <p>Create a new empty, non focusable popup window of dimension (0,0).</p>
     * <p>
     * <p>The popup does not provide any background. This should be handled
     * by the content view.</p>
     */
    public MyPopupWindow() {
    }

    /**
     * <p>Create a new non focusable popup window which can display the
     * <tt>contentView</tt>. The dimension of the window are (0,0).</p>
     * <p>
     * <p>The popup does not provide any background. This should be handled
     * by the content view.</p>
     *
     * @param contentView the popup's content
     */
    public MyPopupWindow(View contentView) {
        super(contentView);
    }

    /**
     * <p>Create a new empty, non focusable popup window. The dimension of the
     * window must be passed to this constructor.</p>
     * <p>
     * <p>The popup does not provide any background. This should be handled
     * by the content view.</p>
     *
     * @param width  the popup's width
     * @param height the popup's height
     */
    public MyPopupWindow(int width, int height) {
        super(width, height);
    }

    /**
     * <p>Create a new non focusable popup window which can display the
     * <tt>contentView</tt>. The dimension of the window must be passed to
     * this constructor.</p>
     * <p>
     * <p>The popup does not provide any background. This should be handled
     * by the content view.</p>
     *
     * @param contentView the popup's content
     * @param width       the popup's width
     * @param height      the popup's height
     */
    public MyPopupWindow(View contentView, int width, int height) {
        super(contentView, width, height);
    }

    /**
     * <p>Create a new popup window which can display the <tt>contentView</tt>.
     * The dimension of the window must be passed to this constructor.</p>
     * <p>
     * <p>The popup does not provide any background. This should be handled
     * by the content view.</p>
     *
     * @param contentView the popup's content
     * @param width       the popup's width
     * @param height      the popup's height
     * @param focusable   true if the popup can be focused, false otherwise
     */
    public MyPopupWindow(View contentView, int width, int height, boolean focusable) {
        super(contentView, width, height, focusable);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 23) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom - statusBarHeight;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    public void showAsDropDown(View anchor, int windowHeight) {
        if (Build.VERSION.SDK_INT >= 23) {
            setHeight(windowHeight);
        }
        super.showAsDropDown(anchor);
    }

    public void setstatusBarHeight(Context context) {
        int statusBarHeight1 = -1;
        int resourceId = getContentView().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = context.getResources().getDimensionPixelSize(resourceId);
        }
        statusBarHeight=statusBarHeight1;
    }
}
